﻿using Bussiness.DEV;
using HslCommunication.Profinet.Siemens;
using Model;
using System;
using System.Collections.Generic;

namespace Bussiness
{
    public class XIMENZI_PLC : IDEV
    {
        public Device Device { get; set; }
        public List<DevicePOI> DevicePOIList { get; set; }
        private SiemensS7Net _siemensPLCS;

        public Result Connect()
        {
            Result result = new Result();
            _siemensPLCS = new SiemensS7Net(SiemensPLCS.S1200, Device.IP) { Port = Device.Port.Value };
            _siemensPLCS.ConnectTimeOut = 3000;
            var _result = _siemensPLCS.ConnectServer();
            result.Success = _result.IsSuccess;
            result.Msg = _result.Message;
            return result;
        }

        public Result<List<Cfg_Field>> Read()
        {
            Result<List<Cfg_Field>> result = new Result<List<Cfg_Field>>();
            foreach (var devPOI in DevicePOIList)
            {
                Cfg_Field cfg_Field = new Cfg_Field();
                cfg_Field.name = devPOI.DevPOIAlias;

                switch ((DevPOIType)devPOI.DevPOIType)
                {
                    case DevPOIType.data_int:
                        var resultInt = _siemensPLCS.ReadInt32(devPOI.Address);
                        if (resultInt.IsSuccess)
                        {
                            result.Success = true;
                            cfg_Field.value = resultInt.Content.ToString();
                        }
                        break;
                    case DevPOIType.data_short:
                        var resultShort = _siemensPLCS.ReadUInt16(devPOI.Address);
                        if (resultShort.IsSuccess)
                        {
                            result.Success = true;
                            cfg_Field.value = resultShort.Content.ToString();
                        }
                        break;
                    case DevPOIType.data_long:
                        var resultLong = _siemensPLCS.ReadInt32(devPOI.Address);
                        if (resultLong.IsSuccess)
                        {
                            result.Success = true;
                            cfg_Field.value = resultLong.Content.ToString();
                        }
                        break;
                    case DevPOIType.data_float:
                        var resultFloat = _siemensPLCS.ReadFloat(devPOI.Address);
                        if (resultFloat.IsSuccess)
                        {
                            result.Success = true;
                            cfg_Field.value = resultFloat.Content.ToString();
                        }
                        break;
                    case DevPOIType.data_string:
                        string[] arr = devPOI.Address.Split('_');
                        ushort _length = Convert.ToUInt16(arr[1]);
                        var resultString = _siemensPLCS.ReadString(devPOI.Address, _length);
                        if (resultString.IsSuccess)
                        {
                            result.Success = true;
                            cfg_Field.value = resultString.Content.ToString();
                        }
                        break;
                    case DevPOIType.data_bit:
                        string[] arr2 = devPOI.Address.Split('.');
                        ushort _index = Convert.ToUInt16(Convert.ToInt32(arr2[0]));
                        var re = _siemensPLCS.Read(arr2[0], 1);
                        if (re.IsSuccess)
                        {
                            result.Success = true;
                            int res_index = 0;
                            if (_index > 7)
                            {
                                res_index = 1;
                                _index -= 8;
                            }

                            int val = (re.Content[res_index] >> _index) & 0x01;
                            cfg_Field.value = val.ToString();
                        }
                        break;
                }

                result.Data.Add(cfg_Field);
            }
            return result;
        }
    }
}
